package com.atguigu.gmall.service.impl;

import com.atguigu.gmall.entity.ProductSalePropertyKey;
import com.atguigu.gmall.entity.ProductSalePropertyValue;
import com.atguigu.gmall.entity.SkuInfo;
import com.atguigu.gmall.entity.SkuSalePropertyValue;
import com.atguigu.gmall.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.awt.event.ItemEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SkuDetailServiceImpl implements SkuDetailService {

    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private SalePropertyKeyService salePropertyKeyService;
    @Autowired
    private SalePropertyValueService salePropertyValueService;
    @Autowired
    private SkuSalePropertyValueService skuSalePropertyValueService;

    @Override
    public SkuInfo getById(Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return skuInfo;
    }

    @Override
    public List<ProductSalePropertyKey> getSkuSalePropertyKeyAndValue(Long productId, Long skuId) {
        List<ProductSalePropertyKey> list = salePropertyKeyService.querySalePropertyByProductId(productId);
        list.stream().forEach(item ->{
            //根据keyid和productID获取value
            packResultValue(item,skuId);
        });
        return list;
    }
    private void packResultValue(ProductSalePropertyKey salePropertyKey, Long skuId) {
        Long productId = salePropertyKey.getProductId();//获取产品ID
        QueryWrapper<ProductSalePropertyValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId).eq("sale_property_key_id", salePropertyKey.getSalePropertyKeyId());
        List<ProductSalePropertyValue> list = salePropertyValueService.list(queryWrapper);
        list.stream().forEach(item -> {
            //设置是否默认选中
            List<SkuSalePropertyValue> listItem = skuSalePropertyValueService.getIsSelecteds(productId,skuId);
            listItem.stream().forEach(item02 ->{
                if (item.getId().equals(item02.getSalePropertyValueId()))
                    item.setIsSelected("1"); // 0 或 1，相等，默认选中
            });
        });
        salePropertyKey.setSalePropertyValueList(list);
    }

    @Override
    public Map getSkuSalePropertyValueId(Long productId) {
        List<SkuSalePropertyValue> list =
                skuSalePropertyValueService.list(new QueryWrapper<SkuSalePropertyValue>().eq("product_id", productId));
        Map<Long, List<SkuSalePropertyValue>> collect = list.stream().collect(Collectors.groupingBy(p -> p.getSkuId()));
        Map<String,String> retMap = packResultMap(collect);
        return retMap;

    }

    private Map<String, String> packResultMap(Map<Long, List<SkuSalePropertyValue>> collect) {
        HashMap<String, String> map = new HashMap<>();
        collect.entrySet().stream().forEach(item->{
            List<SkuSalePropertyValue> value = item.getValue();
            String str = "";
            for (SkuSalePropertyValue skuSalePropertyValue : value) {
              str = str.equals("") ?str.concat(
                      skuSalePropertyValue.getSalePropertyValueId().toString()): str.concat("|" + skuSalePropertyValue.getSalePropertyValueId());
            }
            map.put(str, item.getKey().toString());
        });
        return map;
    }

}
